package textfilewriter.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;

public class DatabaseAccess {

    private static DatabaseAccess instance;
    private static final Logger logger = Logger.getLogger(DatabaseAccess.class.getName());
    private DataSource dataSource;
    private static final String connectURI = "jdbc:oracle:thin:@(description=(address=(host=192.168.16.105)(protocol=tcp)(port=1521))(connect_data=(SERVER = DEDICATED)(service_name=gstt)))";
    private static final String username = "area_service";
    private static final String password = "area_service";
    private static final String driverName = "oracle.jdbc.driver.OracleDriver";

    private DatabaseAccess() {
        dataSource = setupDataSource(connectURI);
    }

    public static DatabaseAccess getInstance() {
        if (instance == null) {
            return new DatabaseAccess();
        } else {
            return instance;
        }
    }

    public static DataSource setupDataSource(String connectURI) {
        BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName(driverName);
        ds.setUsername(username);
        ds.setPassword(password);
        ds.setUrl(connectURI);
        return ds;
    }

    public static void shutdownDataSource(DataSource ds) throws SQLException {
        BasicDataSource bds = (BasicDataSource) ds;
        bds.close();
    }

    public Connection getConnection() throws SQLException {
        return dataSource.getConnection();
//        Connection conn = null;
//        String userName = null;
//        String password = null;
//        try {
//            String url = "jdbc:oracle:thin:@(description=(address=(host=192.168.16.105)(protocol=tcp)(port=1521))(connect_data=(SERVER = DEDICATED)(service_name=gstt)))";
//            userName = "area_service";
//            password = "area_service";
//            DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
//
//            conn = DriverManager.getConnection(url, userName, password);
//            conn.setAutoCommit(false);
//        } catch (SQLException ex) {
//            logger.log(Level.SEVERE, null, ex);
//        }
//        return conn;
    }

    public void closeConnection(Connection cn) {
        if (cn != null) {
            try {
                if (!cn.isClosed()) {
                    cn.close();
                }
            } catch (SQLException e) {
                logger.log(Level.SEVERE, null, e);
            }
        }
    }

    public void commitTransaction(Connection cn) {
        try {
            cn.commit();
        } catch (SQLException e) {
            logger.log(Level.SEVERE, null, e);
        }
    }

    public void rollBackTransaction(Connection cn) {
        try {
            cn.rollback();
        } catch (SQLException e) {
            logger.log(Level.SEVERE, null, e);
        }
    }

    public void closePreparedStatement(PreparedStatement ps) {
        if (ps != null) {
            try {
                ps.close();
            } catch (Exception e) {
                logger.log(Level.SEVERE, null, e);
            }
        }
    }

    public void closeCallableStatement(CallableStatement cs) {
        if (cs != null) {
            try {
                cs.close();
            } catch (Exception e) {
                logger.log(Level.SEVERE, null, e);
            }
        }
    }

    public void closeResultSet(ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                logger.log(Level.SEVERE, null, e);
            }
        }
    }

    public void closeStatement(Statement stmt) {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (Exception e) {
                logger.log(Level.SEVERE, null, e);
            }
        }

    }
}
